package nancal.mp.mvc.es


import io.swagger.annotations.Api
import nancal.mp.db.es.entity.FileLog
import nancal.mp.db.es.esr
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.mvc.OpenAction
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.myoql.db.db
import nbcp.myoql.db.es.RestClientScope
import nbcp.myoql.db.es.component.query
import nbcp.myoql.db.mongo.query
import org.springframework.web.bind.annotation.*

/**
 * Created by CodeGenerator at 2021-04-11 23:42:19
 */
@Api(description = "数据连接", tags = arrayOf("DbConnection"))
@RestController
@OpenAction
@RequestMapping("/dev/es/file-log")
class EsViewFileLogOpenApi {
    @RequestMapping("/list/{id}/{requestId}", method = [RequestMethod.GET, RequestMethod.POST])
    fun list(@PathVariable id: String, @PathVariable requestId: String): ApiResult<List<FileLog>> {
        var conn = mor.dev.esConnection.query()
            .linkOr({ it.id mongoEquals id }, { it.code mongoEquals id })
            .toEntity()
            .must().elseThrow { "找不到数据库连接" }

        var client = db.es.getRestClient(conn.connectionUri);
        usingScope(RestClientScope(client)) {
            var list = esr.system.fileLog.query()
                .must { it.requestId term requestId }
                .orderByAsc { it.recvAt }
                .orderByAsc { it.sort }
                .limit(0, 100)
                .toList()

            return ApiResult.of(list.sortedBy { it.recvAt.AsString() + it.sort.AsString() });
        }
    }
}